#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QTime>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    if(squer==1)
    {
        painter.drawRect(x,y,w,h);
        squer=0;
    }
    if(crcle==1)
    {
        painter.drawEllipse(x,y,w,h);
        crcle=0;
    }
    if(Polygon==1)
    {
        QPointF points[4] = {QPointF(x, y+h), QPointF((x+w)/2, y),QPointF(x+w, y+h)};
        painter.drawPolygon(points,3);
        Polygon=0;
    }
    if(line==1)
    {
        // 反走样 防止绘制出来的线条出现锯齿
            painter.setRenderHint(QPainter::Antialiasing, true);
            // 绘制直线
            painter.drawLine(QPointF(x, y), QPointF(x+w, y+w));
            line=0;
    }
}


void MainWindow::on_pushButton_rectangle_clicked()
{
    x=(ui->lineEdit_x->text()).toInt();
    y=(ui->lineEdit_y->text()).toInt();
    w=(ui->lineEdit_w->text()).toInt();
    h=(ui->lineEdit_h->text()).toInt();
    squer=1;

    QWidget::update();
}

void MainWindow::on_pushButton_circle_clicked()
{
    x=(ui->lineEdit_x->text()).toInt();
    y=(ui->lineEdit_y->text()).toInt();
    w=(ui->lineEdit_w->text()).toInt();
    h=(ui->lineEdit_h->text()).toInt();
    crcle=1;
    QWidget::update();
}



void MainWindow::on_pushButton_triangle_clicked()
{
    x=(ui->lineEdit_x->text()).toInt();
    y=(ui->lineEdit_y->text()).toInt();
    w=(ui->lineEdit_w->text()).toInt();
    h=(ui->lineEdit_h->text()).toInt();
    Polygon=1;
    QWidget::update();
}



void MainWindow::on_pushButton_line_clicked()
{
    x=(ui->lineEdit_x->text()).toInt();
    y=(ui->lineEdit_y->text()).toInt();
    w=(ui->lineEdit_w->text()).toInt();
    h=(ui->lineEdit_h->text()).toInt();
    line=1;
    QWidget::update();
}
